<?php
session_start();
require('../control/showHome.php');
require_once('../control/timeAgo.php');
require_once('../model/artinfo.php');
require_once('../model/homeDao.php');
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>说点吧</title>
  <link rel="stylesheet" href="../style/css/index.css">
  <script src="../style/js/jquery.min.js"></script>
  <link rel="stylesheet" href="../style/css/font-face.css">
  <link rel="stylesheet" href="../style/css/changeInfo.css">
</head>

<body>
  <div class="container">
    <!-- 详细内容 -->
    <!-- 文章列表 -->
    <div class="content">
      <div class="box">
        <?php
        if (isset($_GET['bid']) && trim($_GET['bid']) != '') {
          boardArt($_GET['bid']);
          $bdName = $GLOBALS['board']['board_name'];
          $time = $GLOBALS['board']['created_time'];
          $num = count($GLOBALS['bdArt']);
          $timeAgo = timeAgo($time);
          echo "
        <div class='box-top'>
          <div class='top-logo'>
            <i class='iconfont icon-wenjian'></i>
            <span>说点吧板块</span>
          </div>
          <div class='top-title'>
            <h2>$bdName</h2>
            <div class='top-title-footer'>
              <span><i class='iconfont icon-wenjian'></i>$num</span>
              <span class='timeMsg'><i class='iconfont icon-shizhong'></i>创建时间：$timeAgo<p>$time</p></span>
              
            </div>
          </div>

        </div>

          ";
          echo "
          <div class='box-article'>
            <h4 class='box-article-title'>文章列表($num)</h4>
          <div class='box-article-list'>";
          if (count($GLOBALS['bdArt']) > 0) {
            foreach ($GLOBALS['bdArt'] as $item) {
              $time = timeAgo($item['updated_time']);
              echo <<<bdArt
                 <div class="list-item" data-art="$item[article_id]">
                  <h3>$item[title]</h3>
                  <span class="list-item-board">$item[board_name]</span>

                  <div class="list-item-content">
                    $item[content]
                  </div>

                  <div class="list-item-zan">
                    <span><i class="iconfont icon-dianzan"></i>$item[like_count]</span>
                    <span><i class="iconfont icon-yanjing_xianshi">$item[view_count]</i></span>
                  </div>

                  <div class="list-item-footer">
                    <span class='timeMsg'><i class="iconfont icon-shizhong"></i>$time <p>$item[updated_time]</p></span>
                    <div class="list-item-avatar">
                      <img src="../image/avatar/$item[avatar]" alt="" class="list-item-userImg">
                      <span>$item[uName]</span>
                    </div>
                  </div>
                </div>
                
                bdArt;
            }
          } else {
            echo "<div class='noArt'>该板块还没有文章~</div>";
          }

          echo "</div>
             </div>";
        }

        ?>





      </div>
    </div>

    <!-- 文章主要内容 -->
    <div class="article">

      <?php
      if ($_SERVER['REQUEST_METHOD'] === "GET" && isset($_GET['aid'])) {
        $aid = trim($_GET['aid']);
        if (isset($_COOKIE['aid']) && $_COOKIE['aid'] != 'yes') {
          if (isset($_COOKIE['user']) && trim($_COOKIE['user']) != '') {
            viewCountAdd($aid);
          }
        }
        byIdSelectArt($aid);
        byartSelectCom($aid);
        $info = $GLOBALS['art'];
        // var_dump($info);
        $cnum = count($GLOBALS['commont']);
        $rnum = count($GLOBALS['repArr']);
        $num = $cnum + $rnum;
        $time = $info['published_time'];
        $title = htmlspecialchars($info['title']);
        $content = htmlspecialchars($info['content']);
        $uName = htmlspecialchars($info['uName']);
        $avatar = htmlspecialchars($info['avatar']);
        $likeCount = intval($info['like_count']);
        $viewCount = intval($info['view_count']);

        echo <<<html1
                        <div class="box">

                            <div class="box-top">

                                <div class="box-top-logo">
                                    <i class="iconfont icon-wenjian"></i>
                                    <span>说点吧文章</span>
                                </div>

                                <div class="box-top-msg">
                                     <div class="box-top-left">
                                      
                                        <h3>$title</h3>
                    html1;
        if (isset($_SESSION['uid']) && isUserLike($_SESSION['uid'], $_GET['aid']) == 'yes') {
          echo '<span class="box-top-dz" ><i class="iconfont icon-dianzan_kuai"></i>' . "<span>" . $likeCount . "</span>" . '</span>';
        } else {
          echo '<span class="box-top-dz" ><i class="iconfont icon-dianzan"></i>' . "<span>" . $likeCount . "</span>" . '</span>';
        }


        echo <<<html2
                                    </div>

                                    <ul class="box-top-item">
                                        <li><i class="iconfont icon-yanjing_xianshi"></i> 浏览量 <span>$viewCount</span></li>
                                        <li><i class="iconfont icon-shizhong"></i> 发布于 <span>$time</span></li>
                                        <li><img src="../image/avatar/$avatar" alt=""><span>$uName</span></li>
                                    </ul>

                                </div>

                                <div class="box-top-line"></div>

                            </div>

                            <div class="box-content">
                                <span>文章内容：</span>
                                <p>$content</p>
                                <div class="box-content-line"></div>
                            </div>

                            <div class="box-reply">
                                <span>评论(<span>$num</span>)</span>
                html2;

        if (isset($_SESSION['uName']) && trim($_SESSION['uName']) != '') {
          echo "<div class='box-reply-inp'>
                            <textarea cols='179' rows='5' maxlength='150' placeholder='发一条友善的评论吧~'></textarea>
                            <button class='sendbtn'><i class='iconfont icon-fasong'></i>发表评论</button>
                            <div class='textNum'><span class='wordCount'>0</span>/<span>150</span></div>
                          </div>";
        }
        echo " </div>";
        if (isset($_SESSION['uName']) && trim($_SESSION['uName']) != '') {
          // 评论展示
          if ($cnum > 0) {
            echo '<ul class="user-parent-comment">';
            foreach ($GLOBALS['CRArr'] as $val) {
              $uname = htmlspecialchars($val['uName']);
              $ctime = timeAgo($val['created_time']);
              $comment = htmlspecialchars($val['content']);
              $likes = intval($val['like_count']);
              $cid = htmlspecialchars($val['comment_id']);
              $whoid = htmlspecialchars($val['uid']);
              echo <<<COMMENT
                                <li class="user-reply" data-cid="$cid" data-who="$whoid">
                                    <div class="user-reply-main">
                                        <div class="user-reply-top">
                                            <span>$uname</span>
                                            <span>$ctime</span>
                                        </div>
                                        <p>$comment</p>
                                        <div class="user-reply-footer">
                                            <span class="replySpan">回复</span>
                        COMMENT;
              if (count($val['rep']) <= 0) {
                echo "";
              } else {
                echo "<span class='shou'><i class='iconfont icon-xiangxia1'></i></span>";
              }

              if ($uname == $_COOKIE['user']) {
                echo "<span class='delCom'>删除</span>";
              }
              echo <<<COMMENT2
                                        </div>
                                        <div class="user-reply-footer-com">
                                            <textarea cols="179" rows="5" maxlength="150" placeholder="发一条友善的评论吧~"></textarea>
                                            <button class="repBtn"><i class="iconfont icon-fasong"></i>回复</button>
                                            <div class="textNum"><span class="wordCount">0</span>/<span>150</span></div>
                                        </div>
                                    </div>
                        COMMENT2;

              // 回复列表
              if (isset($val['rep']) && is_array($val['rep'])) {
                echo '<ul class="user-reply-son">';
                // var_dump($val['rep']);
                foreach ($val['rep'] as $rep) {
                  $rname = htmlspecialchars($rep['uName']);
                  $rid = htmlspecialchars($rep['reply_id']);
                  $rtime = timeAgo($rep['created_time']);
                  $rcomment = htmlspecialchars($rep['content']);
                  $rlikes = intval($rep['like_count']);
                  $who = homeSelectUser($rep['comment_who'])['uName'];
                  $cid = htmlspecialchars($rep['comment_id']);
                  $whoid = htmlspecialchars($rep['uid']);

                  echo <<<REPLY
                                        <li class="son-reply" data-cid="$cid" data-who="$whoid" data-rid="$rid">
                                            <div class="son-reply-main">
                                                <div class="son-reply-top">
                                                    <span>$rname <span>:回复$who</span></span>
                                                    <span>$rtime</span>
                                                </div>
                                                <p>$rcomment</p>
                                                <div class="son-reply-footer">
                                                    <span>回复</span>


                        REPLY;

                  if ($rname == $_COOKIE['user']) {
                    echo "<span class='delRep'>删除</span>";
                  }
                  echo <<<REPLY1
                                          </div>
                                                <div class="son-reply-footer-com">
                                                    <textarea cols="179" rows="5" maxlength="150" placeholder="发一条友善的评论吧~"></textarea>
                                                    <button class="sonRepBtn"><i class="iconfont icon-fasong"></i>回复</button>
                                                    <div class="textNum"><span class="wordCount">0</span>/<span>150</span></div>
                                                </div>
                                            </div>
                                        </li>


                        REPLY1;
                }
                echo '</ul>';
              }
              echo '</li>';
            }
            echo "</ul><div class='tofooter'>到底了~</div>";
          } else {
            echo "<ul class='user-parent-comment'></ul>";
            echo "</ul><div class='tofooter'>到底了~</div>";
          }
        } else {
          echo "<div class='noRep'>请先登录~</div>";
        }


        echo '</div>';
      }
      ?>
    </div>


    <!-- 模态框  登录注册-->
    <div class="modal">
      <div class="modal-box">
        <!-- 登录注册顶部 -->
        <div class="modal-head">
          <span class="iconfont icon-morentouxiang">说点吧 </span>
          <span>账户</span>
          <span class=" iconfont icon-cha"></span>
        </div>

        <ul class="modal-list">
          <li class="iconfont icon-denglu-copy list-active">登录</li>
          <li class="iconfont icon-zhuce">注册</li>
          <li class="line"></li>
        </ul>

        <!-- 表单 -->
        <div class="form">
          <!-- 登录 -->
          <div class="login">
            <form action="../control/user.php" method="post">
              <input type="hidden" name="action" value="login">
              <div class="login-name">
                <label for="uName">用户名：</label>
                <br>
                <input type="text" required id="uName" name="uName" placeholder="用户名">
              </div>

              <div class="login-pass">
                <label for="uPass">密码：</label>
                <br>
                <input type="password" required id="uPass" name="uPass" placeholder="密码">
              </div>


              <div class="login-footer">
                <span>还没有账号？</span>
                <span class="goReg">前往注册</span>
                <input type="submit" value="登录">
              </div>
            </form>
          </div>

          <!-- 注册 -->
          <div class="regist">
            <form action="../control/user.php" method="post" enctype="multipart/form-data">
              <input type="hidden" name="action" value="regist">
              <div class="avatar">
                <label for="avatar"><img src="../image/avatar/default.png" alt="点击添加头像"></label>
              </div>
              <input type="file" id="avatar" accept="image/*" style="display: none;" name="avatar" required>
              <div class="regist-name">
                <label for="uName">用户名：</label>
                <br>
                <input type="text" required id="uName" name="uName" placeholder="用户名">
              </div>
              <div class="regist-pass">
                <label for="uPass">密码：</label>
                <br>
                <input type="password" required name="uPass" id="uPass" placeholder="密码">
              </div>
              <div class="regist-agPass">
                <label for="agPass">确认密码：</label>
                <br>
                <input type="password" required id="agPass" name="agPass" placeholder="确认密码">
              </div>
              <div class="regist-footer">
                <input type="submit" value="注册">
              </div>
            </form>
          </div>
        </div>

      </div>
    </div>

    <!-- 头部 -->
    <div class="header">
      <a class="header-title" href="../view/index.php">说点吧</a>
      <?php
      if (isset($_SESSION['uName']) && trim($_SESSION['uName'] != '')) {
        $user = selectUser($_SESSION['uName']);
        echo <<<loginbefore
        <!-- 登录之后 -->
          <div class="user-logining">
            <img src="../image/avatar/$user[avatar]" alt="">
            <span>$user[uName]</span>
            <i class="iconfont icon-xiangxia1"></i>
          </div>
        loginbefore;
      } else {
        echo <<<loginafter
          <!-- 登录之前 -->
          <span class="header-login iconfont icon-morentouxiang">登录 / 注册</span>
        loginafter;
      }
      ?>



      <div class="user">
        <!-- 用户登录之后的下拉框 -->
        <ul>
          <li onclick="window.location.href='../view/userIndex.php'"><i
              class="iconfont icon-morentouxiang"></i><span>个人主页</span></li>
          <li><i class="iconfont icon-xiugai"></i><span>修改信息...</span></li>
          <hr>
          <li class="user-out"><i class="iconfont icon-tuichu"></i><span>退出登录</span></li>
        </ul>
      </div>

    </div>

    <!-- 退出登录的模态框 -->
    <div class="modal-out">
      <div class="modal-out-box">
        <div class="modal-out-box-msg">
          <i class="iconfont icon-tuichu"></i>
          <h2>退出登录</h2>
          <span>确定要退出登录吗？</span>
        </div>
        <form action="../control/user.php" method="get">
          <input type="hidden" name="action" value="out">
          <button type="button" onclick="window.location.href='../view/index.php'">取消</button>
          <input type="submit" value="退出登录">
        </form>
      </div>
    </div>

    <!-- 主要展示 -->
    <div class="center">

      <!-- 左侧渲染文章和板块以及输入框 -->
      <div class="left">
        <!-- 搜索框部分 -->
        <div class="search">
          <!-- 文章与板块的切换 -->
          <div class="search-top">
            <i class="iconfont icon-liebiao"></i>
            <span class="liebiao spanActive">板块</span>
            <i class="iconfont icon-shuxian"></i>
            <span class="shuxian">文章</span>
            <div class="line"></div>
          </div>
          <!--搜索框表单 -->
          <div class="search-form">
            <form action="../control/showHome.php" method="get">
              <input type="hidden" value="search" name="type">
              <input type="hidden" value="bd" name="searchtype" class="search-form-type">
              <input type="hidden" value="hot" name="sort" class="search-form-sort">
              <input type="text" placeholder="输入要搜索的内容" name="search" required class="search-form-input">
              <button type="submit"><i class="iconfont icon-sousuo"></i></button>
            </form>
            <a href="../view/index.php" class="searchAll">全部</a>
          </div>

          <!-- 选择文章时的排序 -->
          <div class="sort">
            <h5>排序：</h5>
            <span class=" sortActive iconfont icon-redu" data-sort="hot">热度</span>
            <span class="iconfont icon-shizhong" data-sort="now">最新</span>
            <span class="iconfont icon-yanjing_xianshi" data-sort="look">访问量</span>
          </div>
        </div>

        <!-- 板块内容 -->
        <div class="board">

          <?php
          if (isset($_GET['search']) && isset($_GET['searchtype']) && $_GET['searchtype'] == 'bd') {
            searchBoard(trim($_GET['search']));
          }

          if (count($GLOBALS['allBoard']) > 0) {
            foreach ($GLOBALS['allBoard'] as $val) {
              $time = timeAgo($val['updated_time']);
              echo <<<board
                    <div class="board-item" data-bid="$val[board_id]">
                      <h2>$val[board_name]</h2>
                      <a href="index.php?bid=$val[board_id]" style="display:none;"></a>
                      <div class="board-footer">
                        <span><i class="iconfont icon-wenjian"></i>$val[num]份文章</span>
                      <span class='timeMsg'><i class="iconfont icon-shizhong"></i>$time <p>$val[updated_time]</p></span>
                      </div>
                    </div>
                board;
            }
            ;
          } else {
            echo "<div class='wu'>亲~没有找到$_GET[search]相关的板块</div>";
          }

          ?>
        </div>


        <!-- 所有文章内容 -->
        <div class="center-article">
          <?php
          if ($_SERVER['REQUEST_METHOD'] == 'GET') {
            if (isset($_GET['search']) && isset($_GET['searchtype']) && $_GET['searchtype'] == 'wz' && isset($_GET['sort']) && trim($_GET['sort']) != '') {
              selectAllArticle(trim($_GET['sort']), trim($_GET['search']));
            }
            if (isset($_GET['sort']) && !isset($_GET['search'])) {
              $sort = $_GET['sort'];
              selectAllArticle($sort, null);
            }
            if (count($GLOBALS['allArticleArr']) > 0) {
              foreach ($GLOBALS['allArticleArr'] as $val) {
                $time = timeAgo($val['updated_time']);
                echo <<<article
                  <div class="center-article-item" data-art="$val[article_id]" >
                    <h3>$val[title]</h3>
                    <span class="list-item-board">$val[board_name]</span>

                    <div class="list-item-content">
                      $val[content]
                    </div>

                    <div class="list-item-zan">
                      <span><i class="iconfont icon-dianzan"></i>$val[like_count]</span>
                      <span><i class="iconfont icon-yanjing_xianshi">$val[view_count]</i></span>
                    </div>

                    <div class="list-item-footer">
                       <span class='timeMsg'><i class="iconfont icon-shizhong"></i>$time <p>$val[updated_time]</p></span>
                      <div class="list-item-avatar"><img src="../image/avatar/$val[avatar]" alt=""
                          class="list-item-userImg"><span>$val[uName]</span></div>
                    </div>
                  </div>
                article;
              }
            } else {
              echo "<div class='wu'>亲~没有找到$_GET[search]相关的文章</div>";
            }
          }


          ?>

        </div>
      </div>

      <!-- 右侧渲染随机文章 -->
      <div class="right">
        <div class="right-add">
          <i class="iconfont icon-jia"></i>
          <span>发表文章</span>
        </div>
        <div class="right-art">
          <div class="article-top">
            <i class="iconfont icon-describe"></i>
            <span>文章推荐</span>
          </div>
          <div class="right-article">
            <ul>
              <?php
              tuijinaArticle();
              foreach ($GLOBALS['tuijianArt'] as $index => $val) {
                $i = $index + 1;
                echo <<<art
                <li class="artLi" data-artId="$val[article_id]" >
                  <span class="artIndex">$i</span>
                  <div>
                    <h3>$val[title]</h3>
                    <p>$val[content]</p>
                    <i class="iconfont icon-yanjing_xianshi">$val[view_count]</i>
                  </div>
                </li>
              art;
              }

              ?>

            </ul>
          </div>
        </div>

      </div>
    </div>

    <!-- 个人信息修改 -->
    <div class="userInfoChange ">
      <div class="uInfo">
        <!-- 修改信息头部 -->
        <div class="uinfo_top">
          <div class="top_left">
            <i class="iconfont icon-morentouxiang"></i>
            <span>修改用户信息</span>
          </div>
          <div class="top_rghit">
            <i class="iconfont icon-cha"></i>
          </div>
        </div>
        <!-- 修改信息切换 -->
        <div class="infoToggle">
          <div class="toSpan">
            <span class="uspanActive"><i class="iconfont icon-xiugaimima"></i>修改密码</span>
            <span><i class="iconfont icon-xiugaitouxiang"></i>修改头像</span>
          </div>
          <div class="toLine"></div>
          <div class="infoMain">
            <!-- 修改密码 -->
            <div class="infomm">
              <span>当前密码*</span>
              <input type="password" placeholder="输入当前的密码" class="oldmm">
              <span>新密码*</span>
              <input type="password" placeholder="输入新密码" class="newmm">
              <span>确认密码*</span>
              <input type="password" placeholder="确认密码" class="qrmm">
            </div>
            <!-- 修改头像 -->
            <div class="infoAvater">
              <div class="avatar">
                <label for="avatar1"><img src="../image/avatar/default.png" alt="点击添加头像"></label>
              </div>
              <input type="file" id="avatar1" accept="image/*" style="display: none;" name="avatar" required>
            </div>
            <div class="infoButton">

              <button><i class="iconfont icon-baocun"></i>保存</button>
            </div>

          </div>
        </div>
      </div>
    </div>

    <div class="dzErr"><span>请登录~</span><i class="iconfont icon-cha"></i></div>
    <div class="saveMsg"><span>保存成功~</span></div>
  </div>

</body>


<script src="../style/js/index.js"></script>
<script src="../style/js/changeInfo.js"></script>
<script src="../style/js/delcom.js"></script>

</html>